package edu.yonsei.iwb.sdt.gui;

import java.awt.Color;

import java.awt.Point;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.action.WidgetAction;
import org.netbeans.api.visual.anchor.AnchorFactory;
import org.netbeans.api.visual.anchor.AnchorShape;
import org.netbeans.api.visual.border.Border;
import org.netbeans.api.visual.border.BorderFactory;
import org.netbeans.api.visual.graph.GraphScene;
import org.netbeans.api.visual.graph.layout.TreeGraphLayout;
import org.netbeans.api.visual.layout.LayoutFactory.ConnectionWidgetLayoutAlignment;
import org.netbeans.api.visual.widget.ComponentWidget;
import org.netbeans.api.visual.widget.ConnectionWidget;
import org.netbeans.api.visual.widget.LabelWidget;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Widget;

/**
 * @author David Kaspar
 */
public class SDTGraphScene extends GraphScene.StringGraph {

    private static final Border BORDER_4 = BorderFactory.createLineBorder(4);
    private LayerWidget mainLayer;
    private LayerWidget connectionLayer;
    private Color color;
    private WidgetAction moveAction = ActionFactory.createMoveAction();
//    private WidgetAction rightSelectAction = ActionFactory.createSelectAction(sp);
    

    public SDTGraphScene() {
        mainLayer = new LayerWidget(this);
        addChild(mainLayer);

        connectionLayer = new LayerWidget(this);
        addChild(connectionLayer);
        this.getActions ().addAction (ActionFactory.createZoomAction ());
        this.getActions ().addAction (ActionFactory.createPanAction ());
//        this.getActions().addAction(ActionFactory.createZoomAction());
        
    }

    protected Widget attachNodeWidget(String node) {
        JLabel text = new JLabel("<html><center>" + node + "</center></html>");
        text.setHorizontalAlignment(SwingConstants.CENTER);
        ComponentWidget clabel = new ComponentWidget(this, text);
        clabel.setBorder(BORDER_4);
        clabel.setOpaque(true);        
        clabel.getActions().addAction(moveAction);
        mainLayer.addChild(clabel);
        return clabel;
    }

    protected Widget attachEdgeWidget(String edge) {
        ConnectionWidget connection = new ConnectionWidget(this);
        
        connection.setLineColor(getEdgeColor());
        connection.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
             
        LabelWidget lw = new LabelWidget(this, edge);
//        lw.setEnabled(true);
        lw.setCheckClipping(true);
        
        lw.setOpaque(true);
        connection.addChild(lw);        
        connection.setConstraint(lw, ConnectionWidgetLayoutAlignment.CENTER, 50);        
        connectionLayer.addChild(connection);

        return connection;
    }

    private Color getEdgeColor() {
        return this.color;
    }

    public void setEdgeColor(Color color) {
        this.color = color;
    }

    protected void attachEdgeSourceAnchor(String edge, String oldSourceNode, String sourceNode) {
        Widget w = sourceNode != null ? findWidget(sourceNode) : null;
        ((ConnectionWidget) findWidget(edge)).setSourceAnchor(AnchorFactory.createRectangularAnchor(w));
    }

    protected void attachEdgeTargetAnchor(String edge, String oldTargetNode, String targetNode) {
        Widget w = targetNode != null ? findWidget(targetNode) : null;
        ((ConnectionWidget) findWidget(edge)).setTargetAnchor(AnchorFactory.createRectangularAnchor(w));
    }
}